package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("category")//一级路径
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    //查询菜品
    @GetMapping("/page")
    public R<Page<Category>> page(Integer page,Integer pageSize){
        Page<Category> page1=new Page<>(page,pageSize);//设置分页值
        categoryService
                .lambdaQuery()//根据条件查询
                .orderByAsc(Category::getSort)//根据排序字段升序
                .orderByDesc(Category::getUpdateTime)//根据修改时间降序
                .page(page1);//分页查询
        return R.success(page1);
    }
    //新增菜品
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增成功");
    }


    //根据id删除分类，检查菜品，套餐关联关系在删除菜品
    @DeleteMapping
    public R<String> delete(Long id){
        categoryService.deleteById(id);
        return R.success("删除成功");
    }

    //修改菜品
@PutMapping
    public R<String> updete(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
}
//查询菜品分类
    @GetMapping("list")
    public R<List<Category>> list(Integer type){
        List<Category> list = categoryService.lambdaQuery()
                .eq(type!=null,Category::getType, type)
                .orderByAsc(Category::getSort)
                .list();

        return R.success(list);
    }
 }
